package io.renren.modules.workjobs.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.CommonBusinessStatus;
import io.renren.common.utils.MyStatusCode;
import io.renren.modules.workjobs.dao.WorkJobDao;
import io.renren.modules.workjobs.entity.WorkJobEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.workjobs.dao.UserPositionDao;
import io.renren.modules.workjobs.entity.UserPositionEntity;
import io.renren.modules.workjobs.service.UserPositionService;


@Service("userPositionService")
public class UserPositionServiceImpl extends ServiceImpl<UserPositionDao, UserPositionEntity> implements UserPositionService {

    @Autowired
    private WorkJobDao workJobDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserPositionEntity> page = this.page(
                new Query<UserPositionEntity>().getPage(params),
                new QueryWrapper<UserPositionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils iPageListPosition(Map<String, Object> params) {

        long currentPage = Long.parseLong((String)params.get("currentPage")) ;
        long size = Long.parseLong((String) params.get("size")) ;

        String userId = (String) params.get("userId");
        String approveId = (String) params.get("approve");
        int ispass = Integer.parseInt((String) params.get("ispass"));

        QueryWrapper<UserPositionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<UserPositionEntity> userPositionEntities = this.baseMapper.selectList(wrapper);

        //workIds
        List<String> workIds = userPositionEntities.stream().map(item -> item.getPositionId()).collect(Collectors.toList());

        QueryWrapper<WorkJobEntity> workJobEntityQueryWrapper = new QueryWrapper<>();
        workJobEntityQueryWrapper.in("id",workIds)
        .eq("job_issue_status", CommonBusinessStatus.POSITION_PUBLISH_HAVE)
        .eq("job_approve_status",CommonBusinessStatus.POSITION_HAVE_APPROVE)
         .eq("job_is_pass",ispass);

        IPage<WorkJobEntity> page = new Page(currentPage,size);
        workJobDao.selectPage(page,workJobEntityQueryWrapper);
        return new PageUtils(page);
    }

    @Override
    public PageUtils iPageListPositionPublish(Map<String, Object> params) {

        long currentPage = Long.parseLong((String)params.get("currentPage")) ;
        long size = Long.parseLong((String) params.get("size")) ;
        String userId = (String) params.get("userId");
        String publishStatus = (String) params.get("publishStatus");

        QueryWrapper<UserPositionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<UserPositionEntity> userPositionEntities = this.baseMapper.selectList(wrapper);
        QueryWrapper<WorkJobEntity> workJobEntityQueryWrapper = new QueryWrapper<>();

        //IdWorks
        List<String> workIds = userPositionEntities.stream().map(item -> item.getPositionId()).collect(Collectors.toList());

        workJobEntityQueryWrapper.in("id",workIds)
        .eq("job_issue_status",publishStatus)
        .in("job_approve_status",CommonBusinessStatus.POSITION_NOT_HAVE_APPROVE);


        IPage<WorkJobEntity> page = new Page(currentPage,size);
        workJobDao.selectPage(page,workJobEntityQueryWrapper);

        return new PageUtils(page);
    }

}